const connection = require('../app/database.js')
// 所有和数据库相关的操作
class userServer {
  // 创建一个用户
  async create(userInfo){
    // 1. 获取用户数据
    const {name , password} = userInfo

    // 2. 拼接 statement
    const statement = 'INSERT INTO users (name , password ) VALUES (? , ?);'

    // 3. 执行sql语句 异步的操作
    const [result] = await connection.execute(statement, [name,password])
    return result
  }

  // 查询是否有该用户
  async findUserByName(name){
    const statement = ' SELECT * FROM users WHERE name = ?; '
    const [result] = await connection.execute(statement, [name] )
    return result
  }

  async updateTokenToUsersServer (token , userId){
    const statement = 'UPDATE users SET token = ? WHERE id = ?;'
    const [result] = await connection.execute(statement , [token , userId] )
    return result
  }

  async updateAvatarUrlToUsersServer (avatar_url , userId){
    const statement = 'UPDATE users SET avatar_url = ? WHERE id = ?;'
    const [result] = await connection.execute(statement , [avatar_url , userId] )
    return result
  }

}
module.exports = new userServer()